package com.heiye.quiz.biz.consumer;

import com.heiye.quiz.biz.constant.MQConstants;
import com.heiye.quiz.biz.constant.RedisKeyConstants;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author: heiye
 * @date: 2025/04/28 下午5:31
 * @version: v1.0.0
 * @description: 延时删除 Redis 问题缓存
 */
@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "interact_group_" + MQConstants.TOPIC_DELAY_DELETE_QUIZ_REDIS_CACHE,
        // 消费的主题 Topic
        topic = MQConstants.TOPIC_DELAY_DELETE_QUIZ_REDIS_CACHE
)
public class DelayDeleteQuizRedisCacheConsumer implements RocketMQListener<String> {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void onMessage(String body) {
        Long quizId = Long.valueOf(body);
        log.info("## 延迟消息消费成功, quizId: {}", quizId);

        // 删除 redis 笔记缓存
        String quizDetailRedisKey = RedisKeyConstants.buildQuizDetailKey(quizId);
        String quizBriefRedisKey = RedisKeyConstants.buildQuizBriefKey(quizId);
        // 批量删除
        redisTemplate.delete(Arrays.asList(quizDetailRedisKey, quizBriefRedisKey));
    }
}
